package com.zzq.my.jetpacks.jectpackapp.utils

import android.annotation.SuppressLint
import android.content.Context
import android.nfc.Tag
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.google.gson.Gson
import com.hjq.shape.view.ShapeButton
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeTextView
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.adapter.AutoSelectItemAdapter
import com.zzq.my.jetpacks.jectpackapp.bean.AutoSelectItem
import com.zzq.my.jetpacks.jectpackapp.ui.dzfs.AccountTypeConstant
import com.zzq.my.jetpacks.jectpackapp.view.DropDownView
import com.zzq.my.jetpacks.jectpackapp.view.DropDownView.DropDownListener
import com.zzq.my.jetpacks.util.setImageDrawable
import com.zzq.my.jetpacks.view.ShadowViewCard


/**
 * 描述：筛选条件封装管理
 * 作者：zhuangzeqin
 * 时间: 2022/9/12-13:48
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
object DropDownViewManager {
    const val TAG = "DropDownViewManager"

    /**
     * ------注释说明----外围实现这个接口----
     */
    interface OnPopupWindowOnClickListener {
        fun onPopupWindowOnClick(dates: Map<String, String>)
    }

    init {


    }

    @SuppressLint("InflateParams", "NotifyDataSetChanged")
    @JvmStatic
    fun accountInFilter(
        context: Context,
        dropDownView: DropDownView,
        tvFilter:TextView,
        autoSelectItemList: List<AutoSelectItem>,
        listMap: HashMap<String, String>,
        onPopupWindowOnClickListener: OnPopupWindowOnClickListener
    ) {
        val expandedView: View =
            LayoutInflater.from(context).inflate(R.layout.shop_incoming_filter, null, false)
        val rv_type = expandedView.findViewById<RecyclerView>(R.id.rv_type)//入账类型
        val tv_filter_title = expandedView.findViewById<TextView>(R.id.tv_filter_title)//入账开始时间
        val stv_date_start = expandedView.findViewById<ShapeTextView>(R.id.stv_date_start)//入账开始时间
        val stv_date_end = expandedView.findViewById<ShapeTextView>(R.id.stv_date_end)//入账结束时间
        val btn_reset = expandedView.findViewById<ShapeButton>(R.id.btn_reset)//重置
        val btn_confirm = expandedView.findViewById<ShapeButton>(R.id.btn_confirm)//确定
        //        View.inflate(context,R.layout.shop_incoming_filter,null);
        val adapter = AutoSelectItemAdapter()
        adapter.setList(autoSelectItemList)
        tv_filter_title.text = "入账时间"
        rv_type.adapter = adapter
        rv_type.addItemDecoration(
            GridSpaceItemDecoration(
                3,
                ShadowViewCard.dp2px(context, 12F).toFloat(),
                ShadowViewCard.dp2px(context, 12F).toFloat()
            )
        )

        adapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position)
            val data = adapter.data
            data.forEachIndexed { index, autoSelectItem ->
                val selectItem = autoSelectItem as AutoSelectItem
                selectItem.isChecked = index == position
            }
            adapter.notifyDataSetChanged()
        }
        //开始时间
        stv_date_start.setOnClickListener {
            Log.d(TAG, "accountInFilter: 开始时间")
        }
        //结束时间
        stv_date_end.setOnClickListener {
            Log.d(TAG, "accountInFilter: 结束时间")
        }
        //重置
        btn_reset.setOnClickListener {
            adapter.cleanCheck()
            stv_date_start.text = ""
            stv_date_end.text = ""
            listMap["type"] = ""
            listMap["beginTime"] = ""
            listMap["endTime"] = ""
        }
        //确定
        btn_confirm.setOnClickListener {
            val item = adapter.getCheckedData()
            listMap["type"] = item?.value ?: ""
            listMap["beginTime"] = stv_date_start.text.toString()
            listMap["endTime"] = stv_date_end.text.toString()
            Log.d(TAG, "accountInFilter: ${Gson().toJson(listMap)}")
            onPopupWindowOnClickListener.onPopupWindowOnClick(listMap)
            dropDownView.collapseDropDown();
        }


        dropDownView.setExpandedView(expandedView);//设置伸展的布局
        //设置监听事件
        dropDownView.dropDownListener = object : DropDownListener {
            override fun onExpandDropDown() {
                //展开
                tvFilter.setImageDrawable(R.mipmap.icon_arrow_up,"right")
            }

            override fun onCollapseDropDown() {
                //折叠
                tvFilter.setImageDrawable(R.mipmap.icon_arrow_down,"right")
            }
        }

        if (dropDownView.isExpanded) {
            dropDownView.collapseDropDown()
        } else {
            dropDownView.expandDropDown()
        }

    }

    @SuppressLint("InflateParams", "NotifyDataSetChanged")
    @JvmStatic
    fun accountOutFilter(
        context: Context,
        dropDownView: DropDownView,
        tvFilter:TextView,
        listMap: HashMap<String, String>,
        onPopupWindowOnClickListener: OnPopupWindowOnClickListener
    ) {
        val expandedView: View = LayoutInflater.from(context).inflate(R.layout.shop_incoming_filter, null, false)
        val rv_type = expandedView.findViewById<RecyclerView>(R.id.rv_type)//入账类型
        val tv_type_title = expandedView.findViewById<TextView>(R.id.tv_type_title)//标题
        rv_type.visibility = View.GONE
        tv_type_title.visibility = View.GONE
        val tv_filter_title = expandedView.findViewById<TextView>(R.id.tv_filter_title)//标题
        val stv_date_start = expandedView.findViewById<ShapeTextView>(R.id.stv_date_start)//入账开始时间
        val stv_date_end = expandedView.findViewById<ShapeTextView>(R.id.stv_date_end)//入账结束时间
        val btn_reset = expandedView.findViewById<ShapeButton>(R.id.btn_reset)//重置
        val btn_confirm = expandedView.findViewById<ShapeButton>(R.id.btn_confirm)//确定
        //        View.inflate(context,R.layout.shop_incoming_filter,null);
        tv_filter_title.text = "到账时间"

        //开始时间
        stv_date_start.setOnClickListener {
            Log.d(TAG, "accountInFilter: 开始时间")
        }
        //结束时间
        stv_date_end.setOnClickListener {
            Log.d(TAG, "accountInFilter: 结束时间")
        }
        //重置
        btn_reset.setOnClickListener {
            stv_date_start.text = ""
            stv_date_end.text = ""
            listMap["beginTime"] = ""
            listMap["endTime"] = ""
        }
        //确定
        btn_confirm.setOnClickListener {
            listMap["beginTime"] = stv_date_start.text.toString()
            listMap["endTime"] = stv_date_end.text.toString()
            Log.d(TAG, "accountOutFilter: ${Gson().toJson(listMap)}")
            onPopupWindowOnClickListener.onPopupWindowOnClick(listMap)
            dropDownView.collapseDropDown();
        }


        dropDownView.setExpandedView(expandedView);//设置伸展的布局
        //设置监听事件
        dropDownView.dropDownListener = object : DropDownListener {
            override fun onExpandDropDown() {
                //展开
                tvFilter.setImageDrawable(R.mipmap.icon_arrow_up,"right")
            }

            override fun onCollapseDropDown() {
                //折叠
                tvFilter.setImageDrawable(R.mipmap.icon_arrow_down,"right")
            }
        }

        if (dropDownView.isExpanded) {
            dropDownView.collapseDropDown()
        } else {
            dropDownView.expandDropDown()
        }

    }
}